﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "ComboBoxDefinition.h"
#include "GbmpNew.h"
#include "ComboBoxEvent.h"
#include "ControlDefinitionChangeEvent.h"
#include <iterator>
#include "IComboBoxHandler.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
static int s_nextComboBoxDataId = 0;


ComboBoxDefinition::ComboBoxDefinition(const std::wstring& id, const std::wstring& caption) 
    : m_pControlItem(NEW_AS_OWNER_PTR(ControlDefinition, id, caption, caption))
    , m_currentIndex(0)
{
    m_pEvent = NEW_AS_OWNER_PTR(ComboBoxEvent, L"ComboBoxEvent", this);
    m_pDefinitionChangeEvent = NEW_AS_OWNER_PTR(ControlDefinitionChangeEvent, L"ComboBoxDefineChangeEvent", this);
}

ComboBoxDefinition::~ComboBoxDefinition()
{
}

std::vector<std::wstring> ComboBoxDefinition::GetAllItems() const
{
    return std::vector<std::wstring>(m_items.begin(), m_items.end());
}

bool ComboBoxDefinition::DeleteItem(int index)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_data.size() == m_items.size(), L"数据有误",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(index < (int)m_items.size(), L"索引有误",L"GDMPLab",L"2024-03-30");
    if (index < 0)
    {
        return false;
    }
    auto itemPos = m_items.begin();
    std::advance(itemPos, index);
    m_items.erase(itemPos);

    auto dataPos = m_data.begin();
    std::advance(dataPos, index);
    m_data.erase(dataPos);

    ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Delete);
    arguments.SetDeleteOrInsertControlDefinitionIndex(index);
    m_pDefinitionChangeEvent->Emit(&arguments);
    return true;
}

bool ComboBoxDefinition::DeleteAllItems()
{
    m_items.clear();
    m_data.clear();
    m_currentIndex = -1;
    ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::DeleteAll);
    m_pDefinitionChangeEvent->Emit(&arguments);
    return true;
}

namespace
{
    class VectorUtils
    {
    public:
        template <typename T>
        static void InsertAtPostion(int index, const T & data, std::vector<T> & vector)
        {
            std::vector<T> resultVector;
            std::copy(vector.begin(), vector.begin() + index, std::back_inserter(resultVector));
            resultVector.push_back(data);
            std::copy(vector.begin() + index, vector.end(), std::back_inserter(resultVector));
            std::swap(vector, resultVector);
        }
    };
}

bool ComboBoxDefinition::InsertItem(int index, const std::wstring &text, Any useData)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_data.size() == m_items.size(), L"数据有误",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS(index < (int)m_items.size(), L"索引有误",L"GDMPLab",L"2024-03-30");

    // 移动数组有代价，但相对其它位置随机操作，插入数据为非频繁操作。
    VectorUtils::InsertAtPostion(index, text, m_items);
    VectorUtils::InsertAtPostion(index, useData, m_data);

    ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Insert);
    arguments.SetDeleteOrInsertControlDefinitionIndex(index);
    arguments.SetComboBoxItemStr(text);
    m_pDefinitionChangeEvent->Emit(&arguments);
    
    return true;
}

bool ComboBoxDefinition::InsertItem(int index, const std::wstring &text)
{
    return InsertItem(index, text, Any());
}

bool ComboBoxDefinition::SetCurrentIndex(int currentIndex)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(currentIndex < (int)m_items.size(), L"索引有误",L"GDMPLab",L"2024-03-30");
    if (currentIndex != m_currentIndex)
    {
        m_currentIndex = currentIndex;
        ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Other);
        arguments.SetModifyControlDefinitionId(GetId());
        m_pDefinitionChangeEvent->Emit(&arguments);
        ComboBoxEventArgs args(ComboBoxEventType::CurrentIndexChanged, GetId(), GetCurrentText(), GetCurrentIndex(), GetCurrentData());

        if (ComboBoxEvent* ctrlEvent = dynamic_cast<ComboBoxEvent*>(GetEvent()))
        {            
            ctrlEvent->Emit(&args);
        }
        //DBG_WARN_AND_RETURN_FALSE_UNLESS(m_pHandler, L"ComboBox控件处理者未设置", L"GDMPLab", L"2024-03-30");
        if (m_handler)
        {
            m_handler.Process(&args);
        }        
    }
    return true;
}

std::wstring ComboBoxDefinition::GetCurrentText() const
{
    DBG_WARN_AND_RETURN_UNLESS(m_data.size() == m_items.size(), L"", L"数据有误",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_UNLESS(m_currentIndex < (int)m_data.size(), L"", L"数据有误",L"GDMPLab",L"2024-03-30");
    if (m_currentIndex < 0)
    {
        return L"";
    }
    if (m_currentIndex > (int)m_items.size() - 1)
    {
        DBG_WARN(L"currentIndex数据有误",L"GDMPLab",L"2024-03-30");
        return L""; 
    }
    return m_items[m_currentIndex];
}

Any gcmp::ComboBoxDefinition::GetCurrentData() const
{
    DBG_WARN_AND_RETURN_UNLESS(m_data.size() == m_items.size(), Any(), L"数据有误",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_UNLESS(m_currentIndex < (int)m_data.size(), Any(), L"数据有误",L"GDMPLab",L"2024-03-30");
    if (m_currentIndex < 0)
    {
        return Any();
    }
    if (m_currentIndex > (int)m_items.size() - 1)
    {
        DBG_WARN(L"currentIndex数据有误",L"GDMPLab",L"2024-03-30");
        return Any();
    }
    return m_data[m_currentIndex];
}

gcmp::IControlDefinitionChangeEvent* gcmp::ComboBoxDefinition::GetControlDefinitionChangeEvent() const
{
    return const_cast<ControlDefinitionChangeEvent*>(m_pDefinitionChangeEvent.get());
}

bool gcmp::ComboBoxDefinition::SetEnabled(bool enabled)
{
    bool res = m_pControlItem->SetEnabled(enabled);
    if (res)
    {
        ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Other);
        m_pDefinitionChangeEvent->Emit(&arguments);
    }
    return res;
}

bool gcmp::ComboBoxDefinition::SetVisible(bool visible)
{
    bool res = m_pControlItem->SetVisible(visible);
    if (res)
    {
        ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Other);
        m_pDefinitionChangeEvent->Emit(&arguments);
    }
    return res;
}

bool gcmp::ComboBoxDefinition::IsVisible() const
{
    if (m_handler)
    {
        return m_handler->IsVisible();
    }
    return m_pControlItem->IsVisible();
}


bool gcmp::ComboBoxDefinition::IsEnabled() const
{
    if (m_handler)
    {
        return m_handler->IsEnabled();
    }
    return m_pControlItem->IsEnabled();
}

bool gcmp::ComboBoxDefinition::SetControlHandler(OwnerPtr<IComboBoxHandler> pControlHandler)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pControlHandler, L"pControlHandler为空",L"GDMPLab",L"2024-03-30");
    bool visible = IsVisible();
    bool enabled = IsEnabled();
    IComboBoxHandler* pHandler = pControlHandler.get();
    m_handler.SetControlHandler(TransferOwnership(pControlHandler));
    if (visible != pHandler->IsVisible() || enabled != pHandler->IsEnabled())
    {
        ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Other);
        m_pDefinitionChangeEvent->Emit(&arguments);
    }
    return true;
}

bool gcmp::ComboBoxDefinition::BlockControlHandler(bool needBlock)
{
    return m_handler.BlockControlHandler(needBlock);
}

bool gcmp::ComboBoxDefinition::AddItem(const std::wstring& item)
{
    return AddItem(item, Any());
}

bool gcmp::ComboBoxDefinition::AddItem(const std::wstring& item, Any useData)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_data.size() == m_items.size(), L"数据有误",L"GDMPLab",L"2024-03-30");
    m_items.push_back(item);
    m_data.push_back(useData);
    ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Add);
    arguments.SetComboBoxItemStr(item);
    m_pDefinitionChangeEvent->Emit(&arguments);
    return true;
}

